<template>
  <div>
    <Card istitle>
      <template #title>修改密码</template>

      <!-- 账号添加from -->
      <template>
        <el-form
          :model="userForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
          size="small"
        >
          <!-- 旧密码 -->
          <el-form-item label="原密码" prop="oldpassword">
            <el-input
              type="password"
              v-model="userForm.oldpassword"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <!-- 新密码 -->
          <el-form-item label="新密码" prop="newpassword">
            <el-input
              type="password"
              v-model="userForm.newpassword"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <!-- 确认新密码 -->
          <el-form-item label="确认新密码" prop="checkpassword">
            <el-input
              type="password"
              v-model="userForm.checkpassword"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <!-- 提交按钮 -->
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </template>
    </Card>
  </div>
</template>

<script>
import { newvalidatePass, validateCheckPass } from '@/utils/validate.js'
import { checkUserPass, editUserPass } from '@/api/user.js'
import local from '@/utils/local.js'
export default {
  data() {
    return {
      userForm: {
        oldpassword: '',
        newpassword: '',
        checkpassword: ''
      },
      rules: {
        oldpassword: [
          { required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
        newpassword: [
          { validator: newvalidatePass('新密码', this), trigger: 'blur' }
        ],
        checkpassword: [
          {
            validator: validateCheckPass(this),
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 提交修改密码
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 发送请求判断旧密码
          const { code } = await checkUserPass({
            oldPwd: this.userForm.oldpassword
          })
          // 判断旧密码是否正确
          if (code === '00') {
            // 如果正确发请求
            const { code } = await editUserPass({
              newPwd: this.userForm.newpassword
            })
            if (code === 0) {
              setTimeout(() => {
                local.cle()
                this.$router.go()
              }, 1000)
            }
          }
        } else {
          // 触发input检测失败
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-input {
  width: 200px;
}
</style>
